package cn.home.resident.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.home.resident.domain.HomeChat;
import cn.home.resident.mapper.HomeChatMapper;
import cn.home.resident.service.IHomeChatService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.home.common.annotation.Log;
import cn.home.common.core.controller.BaseController;
import cn.home.common.core.domain.AjaxResult;
import cn.home.common.enums.BusinessType;


import cn.home.common.utils.poi.ExcelUtil;
import cn.home.common.core.page.TableDataInfo;

/**
 * 聊天Controller
 * 
 * @author homedoctor
 * @date 2025-04-14
 */
@RestController
@RequestMapping("/chat/chat")
public class HomeChatController extends BaseController
{
    @Autowired
    private IHomeChatService homeChatService;
@Autowired
private HomeChatMapper homeChatMapper;
    /**
     * 查询聊天列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chat:list')")
    @GetMapping("/list")
    public TableDataInfo list(HomeChat homeChat)
    {
        startPage();
        List<HomeChat> list = homeChatService.selectHomeChatList(homeChat);
        return getDataTable(list);
    }

    /**
     * 导出聊天列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chat:export')")
    @Log(title = "聊天", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HomeChat homeChat)
    {
        List<HomeChat> list = homeChatService.selectHomeChatList(homeChat);
        ExcelUtil<HomeChat> util = new ExcelUtil<HomeChat>(HomeChat.class);
        util.exportExcel(response, list, "聊天数据");
    }

    /**
     * 获取聊天详细信息
     */
    @PreAuthorize("@ss.hasPermi('chat:chat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(homeChatService.selectHomeChatById(id));
    }

    /**
     * 新增聊天
     */
    @PreAuthorize("@ss.hasPermi('chat:chat:add')")
    @Log(title = "聊天", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomeChat homeChat)
    {
        return toAjax(homeChatService.insertHomeChat(homeChat));
    }

    /**
     * 修改聊天
     */
    @PreAuthorize("@ss.hasPermi('chat:chat:edit')")
    @Log(title = "聊天", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomeChat homeChat)
    {
        return toAjax(homeChatService.updateHomeChat(homeChat));
    }

    /**
     * 删除聊天
     */
    @PreAuthorize("@ss.hasPermi('chat:chat:remove')")
    @Log(title = "聊天", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(homeChatService.deleteHomeChatByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('chat:chat:list')")
    @GetMapping("/chatlist")
    public AjaxResult chatlist(Long receiveId)
    {
        List<Long> longs = homeChatMapper.selectChatList(receiveId);

        return success(longs);
    }
    @PreAuthorize("@ss.hasPermi('chat:chat:list')")
    @GetMapping("/ChatListByuser")
    public AjaxResult ChatListByuser(Long userid,Long doctorid)
    {
        List<HomeChat> homeChats = homeChatMapper.selectChatListByuser(userid, doctorid);

        return success(homeChats);
    }
}

